package com.linktco.common.utils.excel;

import com.linktco.common.utils.excel.handle.*;
import com.linktco.common.utils.excel.style.StylesHandler;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;

/**
 * Excel导入导出工具类
 *
 * @author zhangnx
 * @link https://blog.csdn.net/weixin_53514362/article/details/120159128
 * @link  https://www.cnblogs.com/jiehanshi/p/10944374.html
 */
public class ExcelUtils {

    /**
     * 导出excel
     *
     * @param clazz     要导出的实体类的类对象
     * @param dataList      要导出的数据
     * @param fileName  文件名称
     * @param response  响应
     * @return
     */
    public static <T> void exportExcel(Class<T> clazz, List<T> dataList, String fileName, HttpServletResponse response) {
        exportExcel(clazz, dataList, null, fileName, response, null, null);
    }



    /**
     * 多个sheet导出
     * @param sheetBeanList
     * @param fileName
     * @param response
     * @param stylesHandler
     */
    public static void exportExcel(List<SheetBean> sheetBeanList, String fileName, HttpServletResponse response, StylesHandler stylesHandler) {
        new ExportExcel(sheetBeanList, fileName,stylesHandler).export(response);
    }

    /**
     *
     * @param clazz
     * @param list
     * @param fileName
     * @param stylesHandler
     * @param <T>
     * @return
     */
    public static <T> void exportExcel(Class<T> clazz, List<T> list, String sheetName, String fileName, HttpServletResponse response, StylesHandler stylesHandler) {
        exportExcel(clazz,list, sheetName, fileName, response, stylesHandler, null);
    }

    /**
     *
     * @param clazz
     * @param list
     * @param fileName
     * @param exportFields
     * @param <T>
     * @return
     */
    public static <T> void exportExcel(Class<T> clazz, List<T> list,String sheetName,String fileName, HttpServletResponse response,List<String> exportFields) {
        exportExcel(clazz,list, sheetName, fileName, response, null, exportFields);
    }


    /**
     * 导出excel
     *
     * @param clazz        要导出的实体类的类对象
     * @param dataList         要导出的数据
     * @param sheetName    sheetName
     * @param fileName     文件名称
     * @param response     响应
     * @param stylesHandler    自定义样式
     * @param exportFields 动态导出指定字段策略
     * @return
     */
    public static <T> void exportExcel(Class<T> clazz, List<T> dataList, String sheetName, String fileName, HttpServletResponse response, StylesHandler stylesHandler, List<String> exportFields) {
        new ExportExcel<>(clazz, dataList, sheetName, fileName, stylesHandler, exportFields).export(response);
    }


    /**
     * 分页导出返回MultipartFile
     * @param fileName
     * @param sheetName
     * @param exportHandler
     * @param pageSize
     * @return
     */
    public static  <T> MultipartFile exportExcel(String fileName, String sheetName, ExportHandler exportHandler, int pageSize, Class<T> clazz) {
        return new ExportExcel<>(fileName,sheetName,exportHandler,pageSize,clazz).export();
    }

    public static  <T> void exportExcel(String fileName, String sheetName, int pageSize, Class<T> clazz, HttpServletResponse response,ExportHandler exportHandler) {
         new ExportExcel<>(fileName,sheetName,exportHandler,pageSize,clazz).export(response);
    }

    public static void main(String[] args) {
        ExcelUtils.exportExcel("", "", new ExportHandler() {
            @Override
            public List getDataList(int current, int size) {
                return null;
            }
        },5000,null);
    }














    /**
     * 导入excel数据到项目
     *
     * @param file      excel文件输入流
     * @param clazz   数据承载的类对象
     * @param titleRowNum   复杂表头所占的行数
     * @param importHandler 数据导入的钩子
     * @return
     */
    public static <T> ImportResult importExcel(MultipartFile file, Class<T> clazz, Integer titleRowNum, ImportHandler importHandler) throws Exception {
        BufferedInputStream is = new BufferedInputStream(file.getInputStream());
        return importExcel(is, clazz, null, titleRowNum, null, null, importHandler);
    }

    /**
     * 复杂表头导入
     * @param file
     * @param clazz
     * @param titleRowNum 复杂表头所占的行数
     * @param importHandler
     * @param <T>
     * @return
     * @throws Exception
     */
    public static <T> ImportResult importExcel(File file, Class<T> clazz, Integer titleRowNum, ImportHandler importHandler) throws Exception {
        InputStream is = new FileInputStream(file);
        return importExcel(is, clazz, null, titleRowNum, null, null, importHandler);
    }


    /**
     *
     * @param file
     * @param clazz
     * @param titleRowNum 复杂表头所占的行数
     * @param cacheSize 一次缓存大小
     * @param importHandler
     * @param <T>
     * @return
     * @throws Exception
     */
    public static <T> ImportResult importExcel(MultipartFile file, Class<T> clazz,Integer titleRowNum,Integer cacheSize, ImportHandler importHandler) throws Exception {
        BufferedInputStream is = new BufferedInputStream(file.getInputStream());
        return importExcel(is, clazz, null, titleRowNum, cacheSize, null, importHandler);
    }

    /**
     * 导入excel数据到项目
     *
     * @param is                excel文件输入流
     * @param clazz             数据承载的类对象
     * @param sheetName         sheetName
     * @param titleRowNum       文件内容中标题行数
     * @param cacheSize         集合缓存大小
     * @param saveFailThreshold 连续保存数据失败次数阈值
     * @param importHandler           数据导入的钩子
     * @return
     */
    public static <T> ImportResult importExcel(InputStream is, Class<T> clazz, String sheetName, Integer titleRowNum, Integer cacheSize, Integer saveFailThreshold, ImportHandler importHandler) throws Exception {
        return new ImportExcel<>(is, clazz, sheetName, titleRowNum, cacheSize, saveFailThreshold, importHandler).execute();
    }

}
